package com.common.base.base

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleObserver
import androidx.viewbinding.ViewBinding
import com.common.base.R
import com.common.base.manager.StackManager
import com.common.base.tools.utils.event.BindEventBus
import com.common.base.tools.utils.event.EventBusUtils
import com.common.base.tools.utils.language.MultiLanguageUtil
import com.didi.drouter.annotation.Router
import com.gyf.immersionbar.ktx.immersionBar


/**
 * Created by zsf on 2022/1/20 11:39
 * *******************
 * BaseActivity
 * *******************
 */
abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity(),
    View.OnClickListener {

    val viewBinding: VB by lazy { viewBinding() }

    protected abstract fun viewBinding(): VB

    lateinit var baseActivity: AppCompatActivity
    lateinit var mContext: Context

    lateinit var activityResultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        baseActivity = this
        mContext = peekAvailableContext()!!
        StackManager.getActivityManager().addActivity(this)
        if (this is LifecycleObserver) lifecycle.addObserver((this as LifecycleObserver))
        if (this.javaClass.isAnnotationPresent(BindEventBus::class.java)) {
            EventBusUtils.register(this)
        }
        initStatus()
        initListener()
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(MultiLanguageUtil.attachBaseContext(newBase))
    }

    protected open fun initStatus() {
        immersionBar {
            statusBarDarkFont(true)
            fitsSystemWindows(true)
//            titleBar(viewBinding.root)
            statusBarColor(R.color.colorWhite)
            navigationBarColor(R.color.colorWhite)
        }
    }


    private fun initARouter() {
        try {
            if (this.javaClass.isAnnotationPresent(Router::class.java)) {
                EventBusUtils.unregister(this)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    protected open fun startActivity(cls: Class<*>?, bundle: Bundle? = null) {
        startActivity(newIntent(cls, bundle))
    }

    protected open fun registerForActivityResult(
        callback: (ActivityResult) -> Unit
    ) {
        activityResultLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                callback.invoke(result)
            }
    }

    protected open fun startActivityForResult(cls: Class<*>?, bundle: Bundle? = null) {
        activityResultLauncher.launch(Intent(mContext, cls))
    }


    protected open fun startActivityFinish(cls: Class<*>?, bundle: Bundle? = null) {
        startActivity(newIntent(cls))
        finish()
    }

    //---------------------------------------
    protected open fun newIntent(cls: Class<*>?, bundle: Bundle? = null): Intent? {
        val intent = Intent(this, cls)
        bundle?.let {
            intent.putExtras(bundle)
        }
        return intent
    }

    override fun onDestroy() {
        StackManager.getActivityManager().removeActivity(this)
        if (this.javaClass.isAnnotationPresent(BindEventBus::class.java)) {
            EventBusUtils.unregister(this)
        }
        super.onDestroy()
    }

    //---------------------------------------
    protected open fun getDisplayMetrics(): DisplayMetrics {
        return resources.displayMetrics
    }

    protected open fun getWidthPixels(): Int {
        return getDisplayMetrics().widthPixels
    }

    protected open fun getHeightPixels(): Int {
        return getDisplayMetrics().heightPixels
    }

    override fun onClick(v: View) {}

    protected open fun initListener() {}

}